/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.common.services.kungkhie.validate.xml;

import com.kungkhies.common.services.kungkhie.parse.KungkhieParseException;
import com.kungkhies.common.services.kungkhie.parse.KungkhieParseService;
import com.kungkhies.common.services.kungkhie.parse.xml.KungkhieXmlParseException;
import com.kungkhies.common.services.kungkhie.parse.KungkhieParseService;
import com.kungkhies.common.services.kungkhie.validate.KungkhieValidateException;
import com.kungkhies.common.services.kungkhie.validate.KungkhieValidateService;
import com.kungkhies.common.services.kungkhie.validate.json.KungkhieJsonValidateServiceImpl;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.kungkhies.xml.schema.kungkhie.*;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import javax.inject.Inject;
import org.apache.commons.io.FileUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author clayton
 */
@Service("kungkhieValidateService")
@Transactional(propagation = Propagation.REQUIRED)
public class KungkhieXmlValidateServiceImpl implements KungkhieValidateService
{
    private ArrayList parseErrors = new ArrayList();
    private XmlOptions options = new XmlOptions();
    private KungkhieParseService parseService;
    private KungkhieDocument kungkhieDocument;

    public KungkhieXmlValidateServiceImpl()
    {
    }

    @Autowired
    public KungkhieXmlValidateServiceImpl(KungkhieParseService parseService)
    {
        this.parseService = parseService;
    }

    @Override
    public void validateKungkhie(String kungkhieString) throws KungkhieValidateException
    {
        kungkhieDocument = null;
        try
        {
            kungkhieDocument = parseService.parseKungkhieFromString(kungkhieString);
        }
        catch (KungkhieParseException ex)
        {
            Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void validateKungkhie(File kungkhieFile) throws KungkhieValidateException
    {
        kungkhieDocument = null;
        try
        {
            kungkhieDocument = parseService.parseKungkhieFromFile(kungkhieFile);
        }
        catch (KungkhieParseException ex)
        {
            Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);

            throw new KungkhieValidateException(ex.getMessage());
        }

        validateContent(kungkhieDocument);
    }

    @Override
    public void validateKungkhieFromPath(String pathToFile) throws FileNotFoundException, KungkhieValidateException
    {
        ArrayList validationErrors = new ArrayList();
        XmlOptions validationOptions = new XmlOptions();
        validationOptions.setErrorListener(validationErrors);


        KungkhieDocument kungkhieDoc;
        kungkhieDoc = null;

        kungkhieDoc = createKungkhieDocument(pathToFile, "parse.txt");
        // Do some editing to myDoc.

        // During validation, errors are added to the ArrayList for
        // retrieval and printing by the printErrors method.
        boolean isValid = kungkhieDoc.validate(validationOptions);

        // Print the errors if the XML is invalid.
        if (!isValid)
        {
            Iterator iter = validationErrors.iterator();
            while (iter.hasNext())
            {
                System.out.println(">> " + iter.next() + "\n");
            }
        }
    }

    @Override
    public void validateKungkhie(InputStream is) throws KungkhieValidateException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public File createFileFromString(String fileName, String fileContents)
    {
        File file = new File(fileName);
        BufferedWriter out = null;

        try
        {
            out = new BufferedWriter(new FileWriter(fileName));
            out.write(fileContents);
        }
        catch (Exception e)
        {
            Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, e);
        }
        finally
        {
            try
            {
                out.close();
            }
            catch (IOException ex)
            {
                Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return file;
    }

    public void validateContent(KungkhieDocument kungkhieDocumet) throws KungkhieValidateException
    {
        validatePackage(kungkhieDocumet);
        validateKungkhieType(kungkhieDocumet);
        validateVersion(kungkhieDocumet);
        validateLanguage(kungkhieDocumet);
        validateDisplayName(kungkhieDocumet);
        validateNetworkStructure(kungkhieDocumet);
        validateUrls(kungkhieDocumet);
        validateIlos(kungkhieDocumet);
        validateIloCompleteness(kungkhieDocumet);

    }

    public void validatePackage(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        String packageName = kungkhieDocument.getKungkhie().getPackageName();

        System.out.println("package name = " + packageName);
    }

    public void validateKungkhieType(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        System.out.println("validateKungkhieType");
    }

    public void validateVersion(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        String versionCode = kungkhieDocument.getKungkhie().getVersionCode();

        System.out.println("Version code = " + versionCode);
    }

    public void validateLanguage(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        String language = kungkhieDocument.getKungkhie().getLang();

        System.out.println("Language = " + language);;
    }

    public void validateDisplayName(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        String displayName = kungkhieDocument.getKungkhie().getDisplayName();

        System.out.println("Display name = " + displayName);
    }

    public void validateNetworkStructure(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        System.out.println("validateNetworkStructure");
    }

    public void validateIlos(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        System.out.println("validateIlos");
    }

    public void validateIloCompleteness(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        System.out.println("validateIloCompleteness");
    }

    public void validateUrls(KungkhieDocument kungkhieDocument) throws KungkhieValidateException
    {
        System.out.println("validateUrls");
    }

    public KungkhieParseService getParseService()
    {
        return parseService;
    }

    public void setParseService(KungkhieParseService parseService)
    {
        this.parseService = parseService;
    }

    public ArrayList getParseErrors()
    {
        return parseErrors;
    }

    public void setParseErrors(ArrayList parseErrors)
    {
        this.parseErrors = parseErrors;
    }

    public KungkhieDocument createKungkhieDocument(String pathToFile, String filename)
    {

        KungkhieDocument kungkhieDoc;
        kungkhieDoc = null;

        URL url = null;

        url = getClass().getResource(pathToFile);

        File file = new File(filename);
        try
        {
            FileUtils.copyURLToFile(url, file);
        }
        catch (IOException ex)
        {
            Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        try
        {

            kungkhieDoc = KungkhieDocument.Factory.parse(file);
        }
        catch (IOException ex)
        {
            Logger.getLogger(KungkhieXmlValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (XmlException ex)
        {
            Logger.getLogger(KungkhieJsonValidateServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return kungkhieDoc;

    }

    public XmlOptions getOptions()
    {
        return options;
    }

    public void setOptions(XmlOptions options)
    {
        this.options = options;
    }

    public KungkhieDocument getKungkhieDocument()
    {
        return kungkhieDocument;
    }

    public void setKungkhieDocument(KungkhieDocument kungkhieDocument)
    {
        this.kungkhieDocument = kungkhieDocument;
    }
    
    
}
